package com.xls.box2023.jifen.duihuanjibi;


import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lzz.bottomencapsulation.BuildConfig;
import com.lzz.bottomencapsulation.consts.Consts;
import com.lzz.bottomencapsulation.statusbar.StatusBarHeightView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xls.box2023.Consts.IntentConsts;
import com.xls.box2023.Consts.StateConsts;
import com.xls.box2023.R;
import com.xls.box2023.customcontrol.RoundedImageView;
import com.xls.box2023.entity.JiFenMingXiEntity;
import com.xls.box2023.entity.UserEntity;
import com.xls.box2023.mvp.MVPBaseActivity;
import com.xls.box2023.utils.SPUtils;

import java.math.BigDecimal;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


/**
 * MVPPlugin
 */

public class DuiHuanJiBiActivity extends MVPBaseActivity<DuiHuanJiBiContract.View, DuiHuanJiBiPresenter> implements DuiHuanJiBiContract.View, View.OnClickListener {


    private TextView title_text;
    private TextView title_back;
    private RelativeLayout title_rl;
    private StatusBarHeightView title_rl_content;
    private RoundedImageView youxi_icon;
    private TextView youxi_name;
    private TextView youxi_org;
    private TextView youxi_shouyi_yue_num;
    private RelativeLayout item_youxi;
    private RoundedImageView duihuanjinbi_head;
    private TextView duihuanjinbi_nickname;
    private TextView duihuanjinbi_leve;
    private TextView duihuanjinbi_qianshu;
    private RelativeLayout duihuanjinbi_content;
    private RecyclerView duihuanjilu_duihuanguize;
    private TextView duihuanjilu_duihuanguize_btn;
    private TextView duihuanjibi_duihuanguize_hint;
    private RecyclerView duihuanjilu_guize;
    private LinearLayout duihuanjinbi_guize_rl;

    private ImageView duihuanjibi_activity_img;

    public static void startActivity(Context context, JiFenMingXiEntity jiFenMingXiEntity, String type) {
        Intent intent=new Intent(context,DuiHuanJiBiActivity.class);
        intent.putExtra(IntentConsts.id,jiFenMingXiEntity.getAppId());
        intent.putExtra(IntentConsts.packageName,jiFenMingXiEntity.getPackageName());
        intent.putExtra(IntentConsts.type,type);
        context.startActivity(intent);
    }

    public static void startActivity(Context context, String appId, String packageName, String type) {
        Intent intent=new Intent(context,DuiHuanJiBiActivity.class);
        intent.putExtra(IntentConsts.id,appId);
        intent.putExtra(IntentConsts.packageName,packageName);
        intent.putExtra(IntentConsts.type,type);
        context.startActivity(intent);
    }

    @Override
    public int getView() {
        return R.layout.activity_duihuanjinbi;
    }

    @Override
    public void initView() {

        title_text = findViewById(R.id.title_text);
        title_back = findViewById(R.id.title_back);
        title_rl = findViewById(R.id.title_rl);
        title_rl_content = findViewById(R.id.title_rl_content);
        youxi_icon = findViewById(R.id.youxi_icon);
        youxi_name = findViewById(R.id.youxi_name);
        youxi_org = findViewById(R.id.youxi_org);
        youxi_shouyi_yue_num = findViewById(R.id.youxi_shouyi_yue_num);
        item_youxi = findViewById(R.id.item_youxi);
        duihuanjinbi_head = findViewById(R.id.duihuanjinbi_head);
        duihuanjinbi_nickname = findViewById(R.id.duihuanjinbi_nickname);
        duihuanjinbi_leve = findViewById(R.id.duihuanjinbi_leve);
        duihuanjinbi_qianshu = findViewById(R.id.duihuanjinbi_qianshu);
        duihuanjinbi_content = findViewById(R.id.duihuanjinbi_content);
        duihuanjilu_duihuanguize = findViewById(R.id.duihuanjilu_duihuanguize);
        duihuanjilu_duihuanguize_btn = findViewById(R.id.duihuanjilu_duihuanguize_btn);
        duihuanjibi_duihuanguize_hint = findViewById(R.id.duihuanjibi_duihuanguize_hint);
        duihuanjilu_guize = findViewById(R.id.duihuanjilu_guize);
        duihuanjinbi_guize_rl = findViewById(R.id.duihuanjinbi_guize_rl);
        duihuanjibi_activity_img=findViewById(R.id.duihuanjibi_activity_img);
        title_back.setOnClickListener(this);
        duihuanjilu_duihuanguize_btn.setOnClickListener(this);
        title_text.setText("兑换金币");

        duihuanjilu_guize.setLayoutManager(new LinearLayoutManager(this));
        duihuanjilu_guize.setAdapter(mPresenter.getBiliAdapter());

        duihuanjilu_duihuanguize.setLayoutManager(new GridLayoutManager(this,3));
        duihuanjilu_duihuanguize.setAdapter(mPresenter.getAdapter());
        UserEntity userEntity=SPUtils.getUserInfo();
        if (userEntity!=null){
            duihuanjinbi_nickname.setText(userEntity.getNickname());
            DisplayImageOptions options = new DisplayImageOptions.Builder()
                    .cacheInMemory(false)// 设置下载的图片是否缓存在内存中
                    .cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中
                    .build();
            ImageLoader.getInstance().displayImage(userEntity.getAvatar(),duihuanjinbi_head, options);
        }
    }

    @Override
    public void showData(JiFenMingXiEntity viewData) {
        //        游戏图标
        if (!TextUtils.isEmpty(viewData.getPicture())){
            DisplayImageOptions options = new DisplayImageOptions.Builder()
                    .cacheInMemory(false)// 设置下载的图片是否缓存在内存中
                    .cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中
                    .build();
            ImageLoader.getInstance().displayImage(viewData.getPicture(),youxi_icon, options);
        }

            DisplayImageOptions options = new DisplayImageOptions.Builder()
                    .cacheInMemory(false)// 设置下载的图片是否缓存在内存中
                    .cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中
                    .build();
            ImageLoader.getInstance().displayImage(BuildConfig.BASE_COM_URL_BANNER,duihuanjibi_activity_img, options);


        if (!TextUtils.isEmpty(viewData.getAppName())){
            youxi_name.setText(viewData.getAppName());
        }

        if (!TextUtils.isEmpty(viewData.getTenantName())){
            youxi_org.setText(viewData.getTenantName());
        }

        if (TextUtils.equals(mPresenter.getType(),StateConsts.TiXianType.ShouYi)){
            youxi_shouyi_yue_num.setText((viewData.getScoreStr())+" 元宝");
        }else if (TextUtils.equals(mPresenter.getType(),StateConsts.TiXianType.TuiGuang)){
            youxi_shouyi_yue_num.setText((viewData.getRefferScoreStr())+" 元宝");
        }
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.title_back) {
            finish();
        }else if (view.getId()==R.id.duihuanjilu_duihuanguize_btn){
            mPresenter.duihuan();
        }
    }

    @Override
    public void showTiXianBtnState(boolean b) {
        duihuanjilu_duihuanguize_btn.setEnabled(b);
    }

    @Override
    public void showLevel(UserEntity requestData,JiFenMingXiEntity jiFenMingXiEntity) {
        duihuanjinbi_leve.setText("当前等级： "+requestData.getLevel()+"级");
        String jife="0";
        if (TextUtils.equals(mPresenter.getType(), StateConsts.TiXianType.ShouYi)){
            jife=jiFenMingXiEntity.getScore();
        }else if (TextUtils.equals(mPresenter.getType(), StateConsts.TiXianType.TuiGuang)){
            jife=jiFenMingXiEntity.getRefferScore();
        }

        if (requestData != null && !TextUtils.isEmpty(requestData.getRate())) {
            double score =(Double.parseDouble(jife) * Double.parseDouble(requestData.getRate()))/100000;
            BigDecimal number=new BigDecimal(""+score);
            duihuanjinbi_qianshu.setText(Long.parseLong(jife) / Consts.JiFenDanWei + " 元宝 = " + number.setScale(2,BigDecimal.ROUND_DOWN) + " 元");
        }
    }
}
